﻿using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CutShuFa.AutoAction
{
    public class ActionSaveContours : AAutoAction
    {
        public override string ActionName { get; } = "保存轮廓";

        public override List<Parameter> GetParameters()
        {
            if (parameters == null)
            {
                parameters = new List<Parameter>();
                parameters.Add(new Parameter() { Name = "input", Value = "contours", Type = ParameterType.Mat });
                parameters.Add(new Parameter() { Name = "output", Value = "contours.txt", Type = ParameterType.Mat });
                parameters.Add(new Parameter() { Name = "color", Value = "#FF00FF", Type = ParameterType.Color });
            }
            return parameters;
        }

        public override void Run(AutoActionData data)
        {
            if (data.contours == null)
            {
                return;
            }
            if (this.parameters == null)
            {
                GetParameters();
            }
            string input = parameters.Where(p => p.Name == "input").FirstOrDefault().Value;
            string output = parameters.Where(p => p.Name == "output").FirstOrDefault().Value;

            Scalar color = ToScalar(parameters.Where(p => p.Name == "color").FirstOrDefault().Value);

            Mat mat = new Mat(data.matDic[input].Height, data.matDic[input].Width, data.matDic["src_mat"].Type());
            //mat.SetTo(new Scalar(0, 0, 0));
            Cv2.FillPoly(mat, data.contours, color);

            if (data.matDic[output].Channels() == 1)
            {
                Cv2.CvtColor(mat, mat, ColorConversionCodes.BGR2GRAY);
            }

            data.matDic[output] = mat;
        }
    }
}
